<template>
    <el-dialog title="选择活动" :visible.sync="dialogVisible">
        <div class="form-item" v-if="count !== 1">
            <span class="item-title">所选活动:</span>
            <div class="el-tag-box">
                <el-tag v-for="(item, index) of itemsChecked" :key="item.activityId" :closable="true" @close="delItem(index)">{{item.name}}</el-tag>
            </div>
        </div>
        <div class="filter-container">
            <el-input @keyup.enter.native="search" class="filter-item" placeholder="按名称搜索" v-model="listQuery.name"></el-input>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </div>
        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border @row-click="clickRow">
            <el-table-column align="center" width="50">
                <template slot-scope="scope">
                    <label class="input-radio" v-if="count === 1">
                        <input type="radio" :value="{id: scope.row.activityId, name: scope.row.name}" v-model="itemRadio"/>
                        <span></span>
                    </label>
                    <label class="input-checkbox" v-else>
                        <input type="checkbox" :value="{id: scope.row.activityId, name: scope.row.name}" v-model="itemsChecked"/>
                        <span></span>
                    </label>
                </template>
            </el-table-column>
            <el-table-column align="center" label="活动名称" min-width="110">
                <template slot-scope="scope">
                    <div class="text-justify">
                        <p v-text="scope.row.name"></p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column align="center" label="类型" prop="type" width="80">
                <template slot-scope="scope">
                    <span v-if="scope.row.type===4">打折</span>
                    <span v-if="scope.row.type===5">满减</span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="活动开始时间" prop="startTime">
                <template slot-scope="scope">
                    <span v-text="formatDate(scope.row.startTime, 'yyyy-MM-dd hh:mm:ss')"></span>
                </template>
            </el-table-column>
            <el-table-column align="center" label="活动结束时间" prop="stopTime">
                <template slot-scope="scope">
                    <span v-text="formatDate(scope.row.stopTime, 'yyyy-MM-dd hh:mm:ss')"></span>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-container" v-if="listTotal > 0">
            <el-pagination @current-change="handleCurrentChange" :current-page.sync="listQuery.pageIndex" :page-size="listQuery.pageSize" :total="listTotal" layout="total, prev, pager, next, jumper"></el-pagination>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="btnDisabled" @click="ackChoose">确定</el-button>
            <el-button @click="cancelChoose">取消</el-button>
        </div>
    </el-dialog>
</template>
<script>
    export default {
        name: 'chooseActivity',
        props: {
            count: {
                type: Number,
                default: -1
            },
            check: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        data(){
            return {
                dialogVisible: true,
                itemRadio: {},
                itemsChecked: [],
                listQuery: {
                    name: '',
                    status: 1,
                    pageIndex: 1,
                    pageSize: 10
                },
                listLoading: false,
                list: [],
                listTotal: 0,
                btnDisabled: false
            }
        },
        created() {
            this.getList();
            if (this.count === 1 && this.check.length > 0) {
                this.itemRadio = this.check[0];
            }
        },
        methods: {
            // 获取列表
            getList() {
                this.listLoading = true;
                this.request({
                    url: '/bsnl-activity/activity/query',
                    method: 'post',
                    data: this.listQuery
                }).then(({data, totalCount}) => {
                    this.list = data;
                    this.listTotal = totalCount;
                    this.listLoading = false;
                }).catch(err => {
                    console.log(err);
                    this.list = [];
                    this.listTotal = 0;
                    this.listLoading = false;
                });
            },
            delItem(index){
                this.itemsChecked.splice(index, 1);
            },
            search(){
                this.listQuery.pageIndex = 1;
                this.getList();
            },
            clickRow({activityId, name}){
                if (this.count === 1) {
                    this.itemRadio = {
                        id: activityId,
                        name
                    };
                    return false;
                }
                let index = this.itemsChecked.findIndex(item => item.id === activityId);
                if (index < 0) {
                    this.itemsChecked.push({
                        id: activityId,
                        name
                    });
                } else {
                    this.itemsChecked.splice(index, 1);
                }
            },
            handleCurrentChange(val){
                this.listQuery.pageIndex = val;
                this.getList();
            },
            ackChoose(){
                if(this.count === 1 && this.itemRadio.id){
                    this.itemsChecked = [this.itemRadio]
                }
                if(this.itemsChecked.length < 1){
                    return this.$message.error('请选择活动');
                }
                if(this.count > 0 && this.itemsChecked.length > this.count){
                    return this.$message.error(`最多选择${this.count}个活动`);
                }
                this.$emit('choose', this.itemsChecked);
                this.dialogVisible = false;
                document.body.removeChild(this.$el);
            },
            cancelChoose(){
                this.dialogVisible = false;
                document.body.removeChild(this.$el);
            }
        }
    }
</script>
